﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.BLL;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities.Menu;
using Senparc.Weixin.Entities;
using System.Web.UI.HtmlControls;


namespace MxWeiXinPF.Web.admin.weixin
{
    public partial class wxMenu : MxWeiXinPF.Web.UI.ManagePage
    {
        private WeiXinCRMComm cpp = new WeiXinCRMComm();
        private MenuMgr mMrg = new MenuMgr();
    

        protected void btnDelMenu_Click(object sender, EventArgs e)
        {
            try
            {
                Model.wx_userweixin weiXinCode = base.GetWeiXinCode();
                string error = "";
                string accessTokenOrAppId = this.cpp.getAccessToken(weiXinCode.id, out error);
                if (error != "")
                {
                    base.JscriptMsg(error, "", "Error");
                }
                else
                {
                    WxJsonResult result = CommonApi.DeleteMenu(accessTokenOrAppId);
                    this.GetMenu();
                }
            }
            catch (Exception exception)
            {
                base.JscriptMsg("执行过程出现错误：" + exception.Message, "back", "Error");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Model.manager weiXinCode = base.GetAdminInfo();
                string error = "";
                string accessTokenOrAppId = this.cpp.getAccessToken(weiXinCode.id, out error);
                if (error != "")
                {
                    base.JscriptMsg(error, "", "Error");
                }
                else
                {
                    ButtonGroup buttonData = new ButtonGroup();
                    TextBox box = new TextBox();
                    TextBox box2 = new TextBox();
                    TextBox box3 = new TextBox();
                    IList<BaseButton> collection = new List<BaseButton>();
                    IList<SingleButton> list2 = new List<SingleButton>();
                    for (int i = 0; i < 3; i++)
                    {
                        box = this.FindControl("txtTop" + (i + 1)) as TextBox;
                        box2 = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox;
                        box3 = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox;
                        if (box.Text.Trim() != "")
                        {
                            list2 = new List<SingleButton>();
                            TextBox box4 = new TextBox();
                            TextBox box5 = new TextBox();
                            TextBox box6 = new TextBox();
                            for (int j = 0; j < 5; j++)
                            {
                                box4 = this.FindControl("txtMenu" + (i + 1) + (j + 1)) as TextBox;
                                box5 = this.FindControl(string.Concat(new object[] { "txtMenu", i + 1, j + 1, "Key" })) as TextBox;
                                box6 = this.FindControl(string.Concat(new object[] { "txtMenu", i + 1, j + 1, "Url" })) as TextBox;
                                if (box4.Text.Trim() != "")
                                {
                                    if (box6.Text.Trim() != "")
                                    {
                                        SingleViewButton item = new SingleViewButton
                                        {
                                            name = box4.Text.Trim(),
                                            url = box6.Text.Trim()
                                        };
                                        list2.Add(item);
                                    }
                                    else if (box5.Text.Trim() != "")
                                    {
                                        SingleClickButton button2 = new SingleClickButton
                                        {
                                            name = box4.Text.Trim(),
                                            key = box5.Text.Trim()
                                        };
                                        list2.Add(button2);
                                    }
                                    else
                                    {
                                        base.JscriptMsg("二级菜单的名称和key或者url必填！", "", "Error");
                                        return;
                                    }
                                }
                            }
                            if ((list2 != null) && (list2.Count > 0))
                            {
                                if (list2.Count < 1)
                                {
                                    base.JscriptMsg("子菜单个数为2~5个！", "", "Error");
                                    return;
                                }
                                SubButton button3 = new SubButton(Utils.CutString(box.Text.Trim(), 0x10));
                                button3.sub_button.AddRange(list2);
                                collection.Add(button3);
                            }
                            else
                            {
                                if ((box2.Text.Trim() == "") && (box3.Text.Trim() == ""))
                                {
                                    base.JscriptMsg("若无子菜单，必须填写key或者url值！", "", "Error");
                                    return;
                                }
                                if (box3.Text.Trim() != "")
                                {
                                    SingleViewButton button4 = new SingleViewButton
                                    {
                                        name = box.Text.Trim(),
                                        url = box3.Text.Trim()
                                    };
                                    collection.Add(button4);
                                }
                                else if (box2.Text.Trim() != "")
                                {
                                    SingleClickButton button5 = new SingleClickButton
                                    {
                                        name = box.Text.Trim(),
                                        key = box2.Text.Trim()
                                    };
                                    collection.Add(button5);
                                }
                            }
                        }
                    }
                    buttonData.button.AddRange(collection);
                    WxJsonResult result = CommonApi.CreateMenu(accessTokenOrAppId, buttonData, 0x2710);
                    base.JscriptMsg("菜单提交成功！", "wxMenu.aspx", "Success");
                }
            }
            catch (Exception exception)
            {
                base.JscriptMsg("报错：" + exception.Message, "", "Error");
            }
        }

        protected void FlushAT_Click(object sender, EventArgs e)
        {
            try
            {
                Model.manager weiXinCode = base.GetAdminInfo();
                string error = "";
                this.cpp.FlushAccessToken(weiXinCode.id, out error);
                if (error != "")
                {
                    base.JscriptMsg(error, "", "Error");
                }
                else
                {
                    base.JscriptMsg("Access_Token更新成功！", "wxMenu.aspx", "Success");
                }
            }
            catch (Exception exception)
            {
                base.JscriptMsg("执行过程出现错误：" + exception.Message, "back", "Error");
            }
        }

        private void GetMenu()
        {
            try
            {
                Model.manager weiXinCode = base.GetAdminInfo();
                string error = "";
                string accessToken = this.cpp.getAccessToken(weiXinCode.id, out error);
                if (error != "")
                {
                    base.JscriptMsg(error, "", "Error");
                }
                else
                {
                    GetMenuResult menu = this.mMrg.GetMenu(accessToken);
                    if (menu != null)
                    {
                        List<BaseButton> list = menu.menu.button;
                        int count = list.Count;
                        TextBox box = new TextBox();
                        TextBox box2 = new TextBox();
                        TextBox box3 = new TextBox();
                        for (int i = 0; i < count; i++)
                        {
                            BaseButton button = list[i];
                            if (button != null)
                            {
                                box = this.FindControl("txtTop" + (i + 1)) as TextBox;
                                box2 = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox;
                                box3 = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox;
                                box.Text = button.name;
                                if (button.GetType() != typeof(SubButton))
                                {
                                    if (button.GetType() == typeof(SingleViewButton))
                                    {
                                        box3.Text = ((SingleViewButton)button).url;
                                    }
                                    else
                                    {
                                        box2.Text = ((SingleClickButton)button).key;
                                    }
                                }
                                else
                                {
                                    IList<SingleButton> list2 = ((SubButton)button).sub_button;
                                    if ((list2 != null) && (list2.Count > 0))
                                    {
                                        TextBox box4 = new TextBox();
                                        TextBox box5 = new TextBox();
                                        TextBox box6 = new TextBox();
                                        for (int j = 0; j < list2.Count; j++)
                                        {
                                            box4 = this.FindControl("txtMenu" + (i + 1) + (j + 1)) as TextBox;
                                            box5 = this.FindControl(string.Concat(new object[] { "txtMenu", i + 1, j + 1, "Key" })) as TextBox;
                                            box6 = this.FindControl(string.Concat(new object[] { "txtMenu", i + 1, j + 1, "Url" })) as TextBox;
                                            if (list2[j].GetType() == typeof(SingleViewButton))
                                            {
                                                SingleViewButton button2 = (SingleViewButton)list2[j];
                                                box4.Text = button2.name;
                                                box6.Text = button2.url;
                                            }
                                            else
                                            {
                                                SingleClickButton button3 = (SingleClickButton)list2[j];
                                                box4.Text = button3.name;
                                                box5.Text = button3.key;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.GetMenu();
            }
        }
    }
}